package com.meiyu.live.net;

import android.util.Log;

import com.lidroid.xutils.http.client.multipart.MultipartEntity;
import com.lidroid.xutils.http.client.multipart.content.ByteArrayBody;
import com.lidroid.xutils.http.client.multipart.content.FileBody;
import com.lidroid.xutils.http.client.multipart.content.StringBody;
import com.meiyu.live.BaseApplication;
import com.meiyu.live.net.callback.UploadCallBack;
import com.meiyu.live.net.response.UploadResponse;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created by LI on 2016/8/19.
 */
public class UploadFile implements Runnable {
    private String uploadUrl;
    private File file;
    private byte[] filebyteArray;
    private String body;
    private String fileName;
    private String fileUrl;
    private int position;
    private int type = 0;
    private UploadCallBack uploadCallback;

    public UploadFile(String uploadUrl, String fileUrl, String body, UploadCallBack uploadCallback) {
        this.uploadUrl = uploadUrl;
        this.body = body;
        this.fileUrl = fileUrl;
        this.file = new File(fileUrl);
        this.uploadCallback = uploadCallback;
    }

    public UploadFile(String uploadUrl, byte[] filebyteArray, String fileName, String body, UploadCallBack uploadCallback) {
        this.uploadUrl = uploadUrl;
        this.body = body;
        this.fileName = fileName;
        this.filebyteArray = filebyteArray;
        this.uploadCallback = uploadCallback;
    }

    public UploadFile(int type, int position, String uploadUrl, String fileUrl, String body, UploadCallBack uploadCallback) {
        this(uploadUrl, fileUrl, body, uploadCallback);
        this.type = type;
        this.position = position;
    }

    public UploadFile(int position, String uploadUrl, byte[] filebyteArray, String fileName, String body, UploadCallBack uploadCallback) {
        this.uploadUrl = uploadUrl;
        this.body = body;
        this.filebyteArray = filebyteArray;
        this.fileName = fileName;
        this.uploadCallback = uploadCallback;
        this.position = position;
    }


    @Override
    public void run() {
        if (fileName == null || fileName.trim().equals("")) {
            fileName = file.getName();
        }
        UploadResponse result = new UploadResponse();
        try {
            // 核心应用类
            HttpClient httpClient = new DefaultHttpClient();
            MultipartEntity entity = new MultipartEntity();

            if (file != null) {
                entity.addPart("image", new FileBody(file));
            } else {
                entity.addPart("image", new ByteArrayBody(filebyteArray, fileName));
            }

            entity.addPart("hash", new StringBody(body));

            HttpPost request = new HttpPost(uploadUrl);
            request.setEntity(entity);
            HttpResponse httpResponse = httpClient.execute(request);
            if (httpResponse.getStatusLine().
                    getStatusCode() == 200) {
                String json = EntityUtils.toString(httpResponse.getEntity());
                result = BaseApplication.mGson.fromJson(json, UploadResponse.class);
                result.setFilepath(fileUrl);
                result.setPosition(position);
                result.setType(type);
                Log.e("FILEUPLOAD", json);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        uploadCallback.response(result);
    }

}
